package com.tangpian.sna.core.analysis.lda.model;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;

import com.tangpian.sna.core.model.Result;

@Entity
public class ItemTopic extends Result {

	@Id
	private String itemId;

	private int topicId;

	private double maxProbability;

	@Column(length=1000)
	private String value;

	// Integer 为topic id；
	@Transient
	private Map<Integer, Double> probabilities = new HashMap<Integer, Double>();

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	@Transient
	public Map<Integer, Double> getMapping() {
		return probabilities;
	}

	public void addMapping(int typeId, double probability) {
		probabilities.put(typeId, probability);
		value = getProbabilities();
		maxProbability = getMaxProbability();
		topicId = getBelongedTopic();
	}

	public double getMaxProbability() {
		double max = 0;
		for (int id : probabilities.keySet()) {
			if (probabilities.get(id) > max) {
				max = probabilities.get(id);
			}
		}
		return max;
	}

	public int getBelongedTopic() {
		int maxId = 0;
		double max = 0;
		for (int id : probabilities.keySet()) {
			if (probabilities.get(id) > max) {
				max = probabilities.get(id);
				maxId = id;
			}
		}
		return maxId;

	}

	public String getProbabilities() {
		StringBuffer sb = new StringBuffer();
		for (int id : probabilities.keySet()) {
			sb.append(id + ":" + probabilities.get(id) + " ");
		}
		return sb.toString();
	}

}
